package com.cn.bulu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.cn.bulu.dao.FactoryDao;
import com.cn.bulu.pojo.Goods_special;
import com.cn.bulu.service.FactoryService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.MD5Utils.MD5Utils;

import redis.clients.jedis.Jedis;

@Service("factoryService")
public class FactoryServiceImpl implements FactoryService {
	
	
	@Resource
	private FactoryDao factoryDao;

	Jedis redis = new Jedis("localhost", 6379);// 连接redis
	
	
	//登录
	public BuluResult login(String account, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("account", account);
		map.put("password", password);
		Integer result = factoryDao.selectzhanghao(map);
		BuluResult buluResult = new BuluResult();
		if(result==1){//正确
			//获取类型和id
			Map<String, Object> jieguo = factoryDao.selectType(account);
			String sign = MD5Utils.getUUID();
			
			redis.set(sign, (String)jieguo.get("factoryWorkerId"));
			
			Map<String, Object> send = new HashMap<String, Object>();
			send.put("sign", sign);
			send.put("type", jieguo.get("type"));//'类型:0厂长 1验货工 2裁剪工 3缝纫工 4发货工',
			buluResult.setStatus(0);
			buluResult.setData(send);
		}else{//账号或密码错误
			buluResult.setStatus(1);
			buluResult.setMessage("账号或密码错误!!!");
		}
		return buluResult;
	}
	
	
	//修改密码
	public BuluResult updatePassword(String account, String password) {
		Map<String, Object> map = new HashMap<>();
		map.put("account", account);
		map.put("password", password);
		factoryDao.updatePassword(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	
	
	//查询新订单
	public BuluResult selectXindingdan(Integer page) {
		page = 10*(page-1);
		List<Map<String, Object>> list = factoryDao.selectXindingdan(page);
		BuluResult buluResult = new BuluResult();
		if(list!=null&&list.size()>0){
			for(int a=0;a<list.size();a++){
				//根据订单号查询出有多少个商品
				Integer goodsnum = factoryDao.selectGoodsnum((Integer)list.get(a).get("orderId"));
				list.get(a).put("goodsnum", goodsnum);
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("没有新订单!!!");
		}
		return buluResult;
	}
	
	
	
	//根据订单号查看详情
	public BuluResult selectDxiangqing(Integer orderId) {
		List<Map<String, Object>> list = factoryDao.selectGoods(orderId);
		for(int a=0;a<list.size();a++){
			if((Integer)list.get(a).get("type")==0){//布艺帘
				//判断是什么结构
				if((Integer)list.get(a).get("structure")==0){//单布
					//查询布有几个尺寸
					Integer buchicun = factoryDao.selectGoodsBu((Integer)list.get(a).get("goodsId"));
					if(buchicun==0||buchicun==1){
						//根据商品号查询出尺寸
						Map<String, Object> map = factoryDao.selectGoodsBuchicun((Integer)list.get(a).get("goodsId"));
						list.get(a).put("buwide", map.get("wide"));
						list.get(a).put("buhigh", map.get("high"));
						//判断是否有帘头
						if((Integer)list.get(a).get("shadeHead")==1){
							list.get(a).put("touwide", factoryDao.selectGoodsTouchicun((Integer)list.get(a).get("goodsId")));
						}
					}else{
						list.get(a).put("special", 1);
					}
				}else if((Integer)list.get(a).get("structure")==1){//单纱
					//查询纱有几个尺寸
					Integer shachicun = factoryDao.selectGoodsSha((Integer)list.get(a).get("goodsId"));
					if(shachicun==0||shachicun==1){
						//根据商品号查询出尺寸
						Map<String, Object> map = factoryDao.selectGoodsShachicun((Integer)list.get(a).get("goodsId"));
						list.get(a).put("shawide", map.get("wide"));
						list.get(a).put("shahigh", map.get("high"));
					}else{
						list.get(a).put("special", 1);
					}
				}else{//布加纱
					//查询布有几个尺寸
					Integer buchicun = factoryDao.selectGoodsBu((Integer)list.get(a).get("goodsId"));
					if(buchicun==0||buchicun==1){
						//查询纱有几个尺寸
						Integer shachicun = factoryDao.selectGoodsSha((Integer)list.get(a).get("goodsId"));
						if(shachicun==0||shachicun==1){
							//根据商品号查询出布尺寸
							Map<String, Object> map = factoryDao.selectGoodsBuchicun((Integer)list.get(a).get("goodsId"));
							list.get(a).put("buwide", map.get("wide"));
							list.get(a).put("buhigh", map.get("high"));
							//根据商品号查询出纱尺寸
							Map<String, Object> map1 = factoryDao.selectGoodsShachicun((Integer)list.get(a).get("goodsId"));
							list.get(a).put("shawide", map1.get("wide"));
							list.get(a).put("shahigh", map1.get("high"));
							//判断是否有帘头
							if((Integer)list.get(a).get("shadeHead")==1){
								list.get(a).put("touwide", factoryDao.selectGoodsTouchicun((Integer)list.get(a).get("goodsId")));
							}
						}else{
							list.get(a).put("special", 1);
						}
					}else{
						list.get(a).put("special", 1);
					}
				}
			}else if((Integer)list.get(a).get("type")==1){//成品帘
				//根据成品帘类型id查询出类型
				String fptBody = factoryDao.selectChnegpin((Integer)list.get(a).get("fptId"));
				list.get(a).put("fptBody", fptBody);
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

	
	
	//查询广告
	public BuluResult selectGuanggao() {
		List<String> guanggao = factoryDao.selectGuanggao();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(guanggao);
		return buluResult;
	}
	
	
	
	//商品特殊有多个布尺寸或纱尺寸或帘头尺寸
	public BuluResult selectGoodsDG(Integer goodsId) {
		List<Goods_special> list = factoryDao.selectGoodsDG(goodsId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}
	
	
	
	//商品没有特殊根据商品查询布用量
	public BuluResult selectGoodsYG(Integer goodsId) {
		List<Map<String, Object>> list = factoryDao.selectDosage(goodsId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}
	
	
	//商品特殊制作根据特殊id查询每种布用量
	public BuluResult selectSpecialYL(Integer specialId) {
		List<Map<String, Object>> list = factoryDao.selectSpecialYL(specialId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}
	
	
	
	//根据前端发送的订单状态查找符合的订单
	public BuluResult selectOrders(Integer factoryStatus) {
		List<Map<String, Object>> list = factoryDao.selectOrders(factoryStatus);
		BuluResult buluResult = new BuluResult();
		if(list!=null&&list.size()>0){
			for(int a=0;a<list.size();a++){
				//在这需要计算每个订单商品数量和完成程度
				//根据订单号查询这个订单有多少个商品
				List<Map<String, Object>> goodsNums = factoryDao.selectGoodsnum2((Integer)list.get(a).get("orderId"));
				list.get(a).put("goodsNum", list.size());
				//声明一个商品完成的个数字段
				Integer overGoods = 0;
				//根据订单号查询这个订单中前端传这个状态的完成程度
				for(int i=0;i<goodsNums.size();i++){
					if((Integer)goodsNums.get(i).get("type")==0){//商品为布艺帘
						//查询出该商品需处理的面料总个数
						Integer mianliaoZ = factoryDao.selectMianliaoZ((Integer)goodsNums.get(i).get("goodsId"));
						//查询出跟前端传的状态相同的需处理的面料总个数
						Map<String, Object> map = new HashMap<>();
						map.put("goodsId", goodsNums.get(i).get("goodsId"));
						map.put("status", factoryStatus);
						Integer mianliaoY = factoryDao.selectMianliaoY(map);
						if(mianliaoZ==mianliaoY){
							overGoods++;
						}
					}else{//商品不是布艺帘
						if((Integer)goodsNums.get(i).get("factoryStatus")==factoryStatus){
							overGoods++;
						}
					}
				}
				list.get(a).put("overGoods", overGoods);
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无数据!!!");
		}
		return buluResult;
	}
	
	
	
	
	//根据工厂工人id和状态(可接,已接,已完成)查询数据 status 对应可接0 已接1 已完成2
	public BuluResult selectGOrders(HttpServletRequest request, Integer status) {
		String sign = request.getParameter("sign");
		Integer factoryWorkerId = Integer.parseInt(redis.get(sign));
		//应该怎么判断呢先根据工人id判断工人是什么类型
		Integer type = factoryDao.selectWorderType(factoryWorkerId);
		BuluResult buluResult = new BuluResult();
		//判断是否是缝纫工 其他工人按订单 但是缝纫按商品来
		switch (type) {
		case 1://验货工
			//可接订单就是采购之后新订单0
			if(status==0){//可接 查询数据库
				List<Map<String, Object>> kejie = factoryDao.selectKejie(0);
				if(kejie!=null&&kejie.size()>0){
					for(int a=0;a<kejie.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)kejie.get(a).get("orderId"));
						kejie.get(a).put("goodsNums", goodsNums);
					}
					buluResult.setStatus(0);
					buluResult.setData(kejie);
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}else{//已接或已完成
				//查询数据
				Map<String, Object> map = new HashMap<>();
				map.put("factoryWorkerId", factoryWorkerId);
				map.put("status", status);
				List<Map<String, Object>> list = factoryDao.selectFactoryTask(map);
				if(list!=null&&list.size()>0){
					for(int a=0;a<list.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)list.get(a).get("orderId"));
						list.get(a).put("goodsNums", goodsNums);
					}
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}
			break;
		case 2://裁剪工
			//可接订单就是验货完成状态对应罗马数字1
			if(status==0){//可接 查询数据库
				List<Map<String, Object>> kejie = factoryDao.selectKejie(1);
				if(kejie!=null&&kejie.size()>0){
					for(int a=0;a<kejie.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)kejie.get(a).get("orderId"));
						kejie.get(a).put("goodsNums", goodsNums);
					}
					buluResult.setStatus(0);
					buluResult.setData(kejie);
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}else{//已接或已完成
				//查询数据
				Map<String, Object> map = new HashMap<>();
				map.put("factoryWorkerId", factoryWorkerId);
				map.put("status", status);
				List<Map<String, Object>> list = factoryDao.selectFactoryTask(map);
				if(list!=null&&list.size()>0){
					for(int a=0;a<list.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)list.get(a).get("orderId"));
						list.get(a).put("goodsNums", goodsNums);
					}
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}
			break;
		case 3://缝纫工 
			//缝纫工没有可接是厂长分配
			//缝纫工status没有可接只有已接可已完成
			Map<String, Object> map = new HashMap<>();
			map.put("factoryWorkerId", factoryWorkerId);
			map.put("status", status);
			List<Map<String, Object>> list = factoryDao.selectFengren(map);
			if(list!=null&&list.size()>0){
				for(int a=0;a<list.size();a++){
					//判断是什么结构
					if((Integer)list.get(a).get("structure")==0){//单布
						//查询布有几个尺寸
						Integer buchicun = factoryDao.selectGoodsBu((Integer)list.get(a).get("goodsId"));
						if(buchicun==0||buchicun==1){
							//根据商品号查询出尺寸
							Map<String, Object> map2 = factoryDao.selectGoodsBuchicun((Integer)list.get(a).get("goodsId"));
							list.get(a).put("buwide", map2.get("wide"));
							list.get(a).put("buhigh", map2.get("high"));
							//判断是否有帘头
							if((Integer)list.get(a).get("shadeHead")==1){
								list.get(a).put("touwide", factoryDao.selectGoodsTouchicun((Integer)list.get(a).get("goodsId")));
							}
						}else{
							list.get(a).put("special", 1);
						}
					}else if((Integer)list.get(a).get("structure")==1){//单纱
						//查询纱有几个尺寸
						Integer shachicun = factoryDao.selectGoodsSha((Integer)list.get(a).get("goodsId"));
						if(shachicun==0||shachicun==1){
							//根据商品号查询出尺寸
							Map<String, Object> map2 = factoryDao.selectGoodsShachicun((Integer)list.get(a).get("goodsId"));
							list.get(a).put("shawide", map2.get("wide"));
							list.get(a).put("shahigh", map2.get("high"));
						}else{
							list.get(a).put("special", 1);
						}
					}else{//布加纱
						//查询布有几个尺寸
						Integer buchicun = factoryDao.selectGoodsBu((Integer)list.get(a).get("goodsId"));
						if(buchicun==0||buchicun==1){
							//查询纱有几个尺寸
							Integer shachicun = factoryDao.selectGoodsSha((Integer)list.get(a).get("goodsId"));
							if(shachicun==0||shachicun==1){
								//根据商品号查询出布尺寸
								Map<String, Object> map2 = factoryDao.selectGoodsBuchicun((Integer)list.get(a).get("goodsId"));
								list.get(a).put("buwide", map2.get("wide"));
								list.get(a).put("buhigh", map2.get("high"));
								//根据商品号查询出纱尺寸
								Map<String, Object> map1 = factoryDao.selectGoodsShachicun((Integer)list.get(a).get("goodsId"));
								list.get(a).put("shawide", map1.get("wide"));
								list.get(a).put("shahigh", map1.get("high"));
								//判断是否有帘头
								if((Integer)list.get(a).get("shadeHead")==1){
									list.get(a).put("touwide", factoryDao.selectGoodsTouchicun((Integer)list.get(a).get("goodsId")));
								}
							}else{
								list.get(a).put("special", 1);
							}
						}else{
							list.get(a).put("special", 1);
						}
					}
				}
				buluResult.setStatus(0);
				buluResult.setData(list);
			}else{
				buluResult.setStatus(1);
				buluResult.setMessage("暂无数据!!!");
			}
			break;
		case 4://发货工
			//可接订单就是缝纫完成对应罗马数字3
			if(status==0){//可接 查询数据库
				List<Map<String, Object>> kejie = factoryDao.selectFKejie(3);
				if(kejie!=null&&kejie.size()>0){
					for(int a=0;a<kejie.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)kejie.get(a).get("orderId"));
						kejie.get(a).put("goodsNums", goodsNums);
					}
					buluResult.setStatus(0);
					buluResult.setData(kejie);
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}else{//已接或已完成
				//查询数据
				Map<String, Object> map3 = new HashMap<>();
				map3.put("factoryWorkerId", factoryWorkerId);
				map3.put("status", status);
				List<Map<String, Object>> list3 = factoryDao.selectFactoryTask(map3);
				if(list3!=null&&list3.size()>0){
					for(int a=0;a<list3.size();a++){
						Integer goodsNums = factoryDao.selectGoodsnum((Integer)list3.get(a).get("orderId"));
						list3.get(a).put("goodsNums", goodsNums);
					}
					buluResult.setStatus(0);
					buluResult.setData(list3);
				}else{
					buluResult.setStatus(1);
					buluResult.setMessage("暂无数据!!!");
				}
			}
			break;
		}
		return buluResult;
	}
	
	
	
	//工厂工人点击我要接单(不包括缝纫工 缝纫工为厂长分配任务)
	public BuluResult insertOrderType(HttpServletRequest request, Integer orderId) {
		String sign = request.getParameter("sign");
		Integer factoryWorkerId = Integer.parseInt(redis.get(sign));
		//查询订单是否已经被接单
		Integer result = factoryDao.selectOrderJiedan(orderId);
		BuluResult buluResult = new BuluResult();
		if(result!=null){
			Map<String, Object> map = new HashMap<>();
			map.put("factoryWorkerId", factoryWorkerId);
			map.put("orderId", orderId);
			factoryDao.insertOrderType(map);
			buluResult.setStatus(0);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("手慢一步,该订单已被接取!!!");  
		}
		return buluResult;
	}
}
